package com.thrsdk.utils;



import android.text.TextUtils;

import com.dy.google.gson.Gson;
import com.dy.google.gson.GsonBuilder;
import com.dy.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class JsonUtils {
    public static String beanTostr(Object loginMsg) {
        return new Gson().toJson(loginMsg);
    }

    public static JSONObject beanTojson(Object loginMsg) {
        try {
            return new JSONObject(new Gson().toJson(loginMsg));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }
    public static <Result> Result strTobean(String response, Class<Result> tclass) {
        if (TextUtils.isEmpty(response)) {
            return null;
        }

        return (Result) new Gson().fromJson(response, tclass);
    }

    public static HashMap<String, Object> strTomap(String str) {
        Type type = new TypeToken<HashMap<String, String>>() {
        }.getType();
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        HashMap<String, Object> map = gson.fromJson(str, type);
        return map;
    }

    public static String mapTostr(HashMap<String, String> map) {
        return new Gson().toJson(map);
    }

    public static String mapTostr(Map<String, String> bmap) {
        return new Gson().toJson(bmap);
    }
}
